package com.dushiguang.findhoney.Utils.Timer;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.Button;

import com.dushiguang.findhoney.R;

/**
 * 发送短信后倒计时功能
 * Created by ex-dushiguang201 on 2015-11-16.
 */
public class TimerUtil extends CountDownTimer {
    private Activity mActivity;
    private Button mButton;

    public TimerUtil(Activity activity, Button button, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);

        this.mActivity = activity;
        this.mButton = button;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onTick(long l) {
        mButton.setClickable(false);

        mButton.setText(l / 1000 + "秒后可重新发送");
        mButton.setBackground(mActivity.getResources().getDrawable(R.color.dark_grey));
        Spannable span = new SpannableString(mButton.getText().toString());//获取按钮时间
        span.setSpan(new ForegroundColorSpan(Color.RED),0 ,2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时按钮字体颜色设置为红色
        mButton.setText(span);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onFinish() {
        mButton.setText("重新获取验证码");
        mButton.setClickable(true);
        mButton.setBackground(mActivity.getResources().getDrawable(R.color.light_red));
    }
}
